/**
 * 
 */
package t4j.typeinfo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


class DynamicProxyHandler implements InvocationHandler{

	private Object proxied;
	
	public DynamicProxyHandler(Object proxied){
		this.proxied = proxied;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		
		
		System.out.println("**** proxy: " + proxy.getClass() + ", method: " + method + 
				", args: " + args);
		
		if(args != null)
			for(Object arg : args)
				System.out.println("  " + arg);
		
		return method.invoke(proxied,args);
	}

}

public class SimpleDynamicProxy {

	public static void consumer(Interface iface){//分别调用实例的两个方法
		iface.doSomething();
		iface.somethingElse("bonobo");
	}
	
	public static void main(String[] args) {
		
		RealObject real = new RealObject();
		
		consumer(real);//直接调用被代理对象
		
		Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(),
				new Class[]{Interface.class}, new DynamicProxyHandler(real));
		
		consumer(proxy);//调用动态代理对象
	}
	
}
